package cms.api.solr.index.service;

import cms.api.common.utils.SolrClientUtils;
import cms.api.framework.search.IndexService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Solr单点索引服务实现类
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/6/29 18:00
 */
@Slf4j
@Service("solrCoreServiceImpl")
public class SolrCoreServiceImpl extends BaseSolrServiceImpl implements IndexService {

    @Override
    public boolean createIndex(String index) {
        try {
            if (isIndexExist(index)) {
                log.info("要创建的索引[{}]已存在", index);
                return true;
            }
            CoreAdminRequest.Create req = new CoreAdminRequest.Create();
            req.setCoreName(index);
            CoreAdminResponse coreAdminResponse = req.process(solrClient);
            JSONObject responseJsonObject = SolrClientUtils.formatNamedList(coreAdminResponse.getResponse());
            log.info("创建[{}]索引库响应消息[{}]", index, responseJsonObject);
            int status = coreAdminResponse.getStatus();
            return status == NumberUtils.INTEGER_ZERO.intValue();
        } catch (SolrServerException e) {
            log.error("Solr服务器通信/解析异常, index=[{}]", index, e);
        } catch (IOException e) {
            log.error("IO异常, index=[{}]", index, e);
        }
        return false;
    }

    @Override
    public boolean deleteIndex(String index) {
        try {
            if (!isIndexExist(index)) {
                log.info("要删除的索引[{}]不存在", index);
                return true;
            }
            CoreAdminResponse coreAdminResponse = CoreAdminRequest.unloadCore(index, solrClient);
            JSONObject responseJsonObject = SolrClientUtils.formatNamedList(coreAdminResponse.getResponse());
            log.info("删除[{}]索引库响应消息[{}]", index, responseJsonObject);
            int status = coreAdminResponse.getStatus();
            return status == NumberUtils.INTEGER_ZERO.intValue();
        } catch (SolrServerException e) {
            log.error("Solr服务器通信/解析异常, index=[{}]", index, e);
        } catch (IOException e) {
            log.error("IO异常, index=[{}]", index, e);
        }
        return false;
    }

    @Override
    public boolean isIndexExist(String index) {
        try {
            CoreAdminResponse coreAdminResponse = CoreAdminRequest.getStatus(index, solrClient);
            JSONObject responseJsonObject = SolrClientUtils.formatNamedList(coreAdminResponse.getResponse());
            log.info("判断[{}]索引库是否存在响应消息[{}]", index, responseJsonObject);
            return !responseJsonObject.getJSONObject("status").getJSONObject(index).isEmpty();
        } catch (SolrServerException e) {
            log.error("Solr服务器通信/解析异常, index=[{}]", index, e);
        } catch (IOException e) {
            log.error("IO异常, index=[{}]", index, e);
        }
        return false;
    }

}
